package com.congee02.modelshellmvn.advice;

import com.congee02.modelshellmvn.exception.HandledRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = RuntimeException.class)
    public UniformMessage handleException(RuntimeException exception) {
        if (exception instanceof HandledRuntimeException) {
            return UniformMessage.fail(exception.getMessage());
        }
        log.error("发生了一个未处理的异常", exception);
        return UniformMessage.fail("未知错误，请联系管理员");
    }

}
